package com.example.spikeRuota;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class WheelBubbles extends View{

	final static int dimension = 220;
	Paint paint;
	Path path;
	int[] colors; 
	float[] cx;
	float[] cy;
	int[] radius;
	float startAngle;
	
	public WheelBubbles(Context context, AttributeSet attrs) {
		super(context, attrs);
		paint = new Paint();
		paint.setAntiAlias(true);
		path = new Path();
		colors = new int[] {
				getResources().getColor(R.color.bubble1),
				getResources().getColor(R.color.bubble2),
				getResources().getColor(R.color.bubble3),
				getResources().getColor(R.color.bubble4),
				getResources().getColor(R.color.bubble5)
		};
		cx = new float[5];
		cy = new float[5];
		
		double angle = 18*Math.PI/180;
		double start = angle/2;
		radius = new int[]{15, 20, 30, 20, 15};
		
		for (int i=0; i<5; i++){
			cx[i] = (float) ((dimension) * (1 - Math.cos(start + i*angle)))+30-radius[i];
			cy[i] = (float) ((dimension) * (1 - Math.sin(start + i*angle)))+30-radius[i];	
		}
		
		this.setOnTouchListener(new View.OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction()==MotionEvent.ACTION_DOWN){
					startAngle = (float) Math.atan((int)event.getX()-(int)event.getY());

				}
				else if (event.getAction()==MotionEvent.ACTION_UP){
					float endAngle = (float) Math.atan((int)event.getX()-(int)event.getY());
					float angle = startAngle - endAngle;
					
					if(angle > 0) shiftColors(1);
					else shiftColors(-1);
					invalidate();
				}
				return true;
			}

			private void shiftColors(int dir) {
				if (dir==-1){
					int col = colors[4];
					for (int i=4; i>0; i--){
						colors[i] = colors[i-1];
					}
					colors[0] = col;
				}
				else{
					int col = colors[0];
					for (int i=0; i<4; i++){
						colors[i] = colors[i+1];
					}
					colors[4] = col;
				}
			}
		});
		
	}
	
	
	@Override
	protected void onDraw(Canvas c){
		for (int i=0; i<5; i++){
			path.reset();
			path.addCircle(cx[i], cy[i], radius[i], Direction.CW);
			paint.setColor(colors[i]);
			c.drawPath(path, paint);			
		}
	}
	
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(260, 260);
		setMeasuredDimension(260, 260);
	}
	
	
	

}
